<?php 
class CmemberAction extends AdminPublicAction{
	public function index(){
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = 'join_date';//I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "join_date";
			$_GET['updown'] = $upDown = "asc";
				
		}
		
		$status = I('status',-1);
		
		// 地区筛选
		$cid = I('provcode','');
		$qid = I('citycode','');
		$aid = I('area_id','');
		$where = ' 1';
		if($cid > 0){
			$where .= ' AND provcode='. $cid;
		}
		if($qid > 0){
			$where .= ' AND citycode='.$qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$where .= ' AND area_id ='. $aid;
		}
		
		$this->citycode = $qid;
		$this->provcode = $cid;
		$this->area_id = $aid;
		
		//篩選狀態
		if($status >= 0 ) $where .= " AND status=". $status;
		
		$year = I('year','');
		$month = I('month');
		$day = I('day');
		if($day!=0){
			$where.=" AND DATE_FORMAT(join_date,'%d')=".$day;
		}
		if($month!=0){
			$where.=" AND DATE_FORMAT(join_date,'%m')=".$month;
		}
		if($year!=0){
			$where.=" AND DATE_FORMAT(join_date,'%Y')=".$year;
		}
		$this->year = $year>0?$year:'allYear';
		$this->month = $month>0?$month:'allMonth';
		$this->day = $day>0?$day:'allDay';
		//echo $year;die;
		import("ORG.Util.Page");
		$page = 10;
		$count = D('Membership')->getMc();
		//var_dump($count);
		//echo $count;die;
		$Page = new Page($count, $page);
		
		$infoList = D('Membership')->getMcByday($where,$Page->firstRow. ',' . $Page->listRows,$sort." ".$upDown);
		//var_dump($infoList);die;
		
		$this->category = $category;
		$this->status = $status;
		$this->cate = $cate;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_Cmember = 'current';
		$this->display();
		
	}
	/**
	 * 获取统计数据内的详细信息
	 */
	public function detail(){
		$day = I('day');
		if(!$day){
			$this->redirect('index');
		}
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = 'join_date';//I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "join_date";
			$_GET['updown'] = $upDown = "desc";
				
		}
		
		$status = I('status',-1);
		
		// 地区筛选
		$cid = I('provcode','');
		$qid = I('citycode','');
		$aid = I('area_id','');
		$where = ' 1';
		if($cid > 0){
			$where .= ' AND provcode='. $cid;
		}
		if($qid > 0){
			$where .= ' AND citycode='.$qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$where .= ' AND area_id ='. $aid;
		}
		if($cid>0){
			$this->diqu = D('Area')->getAllName($cid,$qid,$aid);
		}
		$this->citycode = $qid;
		$this->provcode = $cid;
		$this->area_id = $aid;
		
		//篩選狀態
		if($status >= 0 ) $where .= " AND status=". $status;
		
		$where.=" AND DATE_FORMAT(join_date,'%Y-%m-%d')='".$day."'";
		//var_dump($where);die;
		import("ORG.Util.Page");
		$page = 10;
		$count = count(D('Membership')->getMcDetail($where,'',$sort." ".$upDown));
		//echo $count;die;
		//var_dump($count);
		//echo $count;die;
		$Page = new Page($count, $page);
	
		$infoList = D('Membership')->getMcDetail($where,$Page->firstRow. ',' . $Page->listRows,$sort." ".$upDown);
		//var_dump($infoList);die;
		$this->day = $day;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_Cproducts = 'current';
		$this->display();
	}
	
	/*
	 * 导出信息
	 */
	public function ToExcel(){
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = 'join_date';//I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "join_date";
			$_GET['updown'] = $upDown = "desc";
				
		}
		
		$status = I('status',-1);
		
		// 地区筛选
		$cid = I('provcode','');
		$qid = I('citycode','');
		$aid = I('area_id','');
		$where = ' 1';
		if($cid > 0){
			$where .= ' AND provcode='. $cid;
		}
		if($qid > 0){
			$where .= ' AND citycode='.$qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$where .= ' AND area_id ='. $aid;
		}
		
		//篩選狀態
		if($status >= 0 ) $where .= " AND status=". $status;
		
		$year = I('year','');
		$month = I('month');
		$day = I('day');
		if($day!=0){
			$where.=" AND DATE_FORMAT(join_date,'%d')=".$day;
		}
		if($month!=0){
			$where.=" AND DATE_FORMAT(join_date,'%m')=".$month;
		}
		if($year!=0){
			$where.=" AND DATE_FORMAT(join_date,'%Y')=".$year;
		}
		
		$result = D('Membership')->getMcByday($where,'',$sort." ".$upDown);
		
	
		 /* echo "<pre>";
		var_dump($result);die; */
	
		$xls_name = "会员统计列表";
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:attachment;filename=$xls_name.xls");
	
		$table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"	xmlns="http://www.w3.org/TR/REC-html40">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <style id="Classeur1_16681_Styles"></style>
            </head>';
		$table .= '<table style="border:1px solid #000; border-spacing:0px; width:1000px;text-align:left;border-collapse: collapse;">';
		$table .= '<thead><tr style="background:#c6c6c6;height:40px;"><th style="border:1px solid #000;">日期</th><th style="border:1px solid #000;">数量</th></thead>';
		$table .= '<tbody>';
		foreach($result as $key=>$value){
			if(($key+1)%2==0){
				$table .= "<tr style='background:#a9c6c9'>";
			}else{
				$table .= "<tr>";
			}
	
			$table .= "<td style='border:1px solid #000;'>".$value['day']."</td>";
			$table .= "<td style='border:1px solid #000;'>".$value['c'].'</td>';			
			$table .= '</tr>';
		}
		$table .= '</tbody>';
		$table .= '</table>';
		echo $table;
	}
}
?>